package com.yaojiaxun.logisticsPlatformSystem.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yaojiaxun.logisticsPlatformSystem.dto.PageDTO;
import com.yaojiaxun.logisticsPlatformSystem.dto.RouteInfoDTO;
import com.yaojiaxun.logisticsPlatformSystem.entity.RouteInfo;
import com.yaojiaxun.logisticsPlatformSystem.entity.RouteSearch;
import com.yaojiaxun.logisticsPlatformSystem.mapper.RouteInfoMapper;
import com.yaojiaxun.logisticsPlatformSystem.util.SnowFlakeIDWorker;
import com.yaojiaxun.logisticsPlatformSystem.util.TimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: yjx_j
 * @Date: 2019/1/28 13:54
 * @Description:
 */
@Service
public class RouteInfoService {

    @Autowired
    private RouteInfoMapper routeInfoMapper;

    public RouteInfo findRouteInfo(Long routeId){
        return routeInfoMapper.findRouteById(routeId);
    }

    public Boolean addRouteInfo(RouteInfo routeInfo){
        routeInfo.setRouteId(new SnowFlakeIDWorker(6, 1).nextId());
        routeInfo.setCreateTime(Instant.now().toEpochMilli());
        routeInfo.setUpdateTime(Instant.now().toEpochMilli());
        return routeInfoMapper.insertRoute(routeInfo)>0;
    }

    public Boolean deleteRouteInfo(Long routeId){
        return routeInfoMapper.deleteRouteById(routeId)>0;
    }

    public Boolean updateRouteInfo(RouteInfo routeInfo){
        routeInfo.setUpdateTime(Instant.now().toEpochMilli());
        return routeInfoMapper.updateRouteByRouteId(routeInfo)>0;
    }

    public RouteInfoDTO beanToDTO(RouteInfo routeInfo){
        if (routeInfo == null)
            return null;
        RouteInfoDTO routeInfoDTO = new RouteInfoDTO();
        BeanUtils.copyProperties(routeInfo, routeInfoDTO);
        routeInfoDTO.setCreateTime(TimeFormat.convertTime(routeInfo.getCreateTime()));
        routeInfoDTO.setUpdateTime(TimeFormat.convertTime(routeInfo.getUpdateTime()));
        return routeInfoDTO;
    }

    public PageDTO<RouteInfoDTO> findPage(Integer pageNum){
        PageDTO<RouteInfoDTO> pageDTO = new PageDTO<>();
        Page<RouteInfoDTO> page = new Page<>(pageNum, 10);
        List<RouteInfoDTO> results = routeInfoMapper.selectPage(page, null).stream().map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setResult(results);
        pageDTO.setPage(page);
        return pageDTO;
    }

    public List<RouteInfo> findRouteByCompany(String routeCompany){
        List<RouteInfo> routes = routeInfoMapper.findRouteByComName(routeCompany);
        return routes;
    }

    //取后6条数据
    public List<RouteInfo> findLast6Route(){
        List<RouteInfo> routes = routeInfoMapper.findLast6Route();
        return routes;
    }

    public RouteInfoDTO findRouteInfoTimeFormat(Long routeId){
        RouteInfo routeInfo = routeInfoMapper.findRouteById(routeId);
        RouteInfoDTO routeInfoDTO = new RouteInfoDTO();
        BeanUtils.copyProperties(routeInfo, routeInfoDTO);
        routeInfoDTO.setUpdateTime(TimeFormat.convertTime2(routeInfo.getUpdateTime()));
        return routeInfoDTO;
    }

    //根据出发地和到达地选择物流专线（没有翻页功能）
    public List<RouteInfo> findRouteByRAndD(String rProvince, String rCity, String rCountry, String dProvince, String dCity, String dCountry){
        List<RouteInfo> routes = routeInfoMapper.findRouteByRAndD(rProvince, rCity, rCountry, dProvince, dCity, dCountry);
        return routes;
    }

    //根据出发地和到达地选择物流专线（带翻页功能）
    public PageDTO<RouteInfoDTO> findPage(Integer pageNum, RouteSearch routeSearch){
        PageDTO<RouteInfoDTO> pageDTO = new PageDTO<>();
        Page<RouteInfoDTO> page = new Page<>(pageNum, 10);
        List<RouteInfoDTO> result = routeInfoMapper.selectPage(page, getWrapper(routeSearch)).stream().map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setResult(result);
        pageDTO.setPage(page);
        return pageDTO;
    }

    public Wrapper<RouteInfo> getWrapper(RouteSearch routeSearch){
        if (routeSearch == null)
            return null;
        Wrapper<RouteInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("route_resource_province", routeSearch.getRProvince());
        wrapper.eq("route_resource_city", routeSearch.getRCity());
        wrapper.eq("route_resource_country", routeSearch.getRCountry());
        wrapper.eq("route_destination_province", routeSearch.getDProvince());
        wrapper.eq("route_destination_city", routeSearch.getDCity());
        wrapper.eq("route_destination_country", routeSearch.getDCountry());
        return wrapper;
    }

}
